//
//  SearchParamRowCellView.swift
//  Hermes
//
//  Created by FH on 2021/2/18.
//  Copyright © 2021 Agora. All rights reserved.
//

import Foundation

class SearchParamRowCellView : NSTableCellView, FLTableRowCellable {
    let yoga = FLContext()
    let lblParamsKey = FLLabel()
    let lblNamespace = FLLabel()
    var bgView: NSView! = nil
    
    static func cellHeight(by dataItem: Any) -> CGFloat {
        return 36
    }
    
    func updateData(_ data: Any) {
        if let item = data as? AstNode {
            self.lblParamsKey.flText = item.cppPrivateParamKey
            self.lblNamespace.flText = item.cppPrivateParamNamespace
        }
    }
    
    func render(_ table: NSTableView) {
        if self.bgView == nil {
            self.bgView = NSView()
            self.bgView.bindAsRoot(to: yoga) { it in
                it.paddingLeft = 2
                it.width = table.bounds.size.width.toFLValue()
                it.height = 36
            }
            
            self.lblNamespace.bind(to: yoga, self.bgView) { it in
                it.width = 300
                it.marginTop = 3
            }
            self.lblNamespace.font = NSFont.systemFont(ofSize: 10)
            self.lblNamespace.textColor = .lightGray
            
            self.lblParamsKey.bind(to: yoga, self.bgView) { it in
                it.width = 330
                it.marginBottom = 4
            }
            self.lblParamsKey.font = NSFont.systemFont(ofSize: 13)
            self.lblParamsKey.textColor = .darkGray
            
            yoga.apply()
            self.addSubview(self.bgView)
        }
    }
    
    // TODO: show full api name when width > 420
    func startMouseOver() {
        self.bgView.flBackgroundColor = .lightGray
        self.lblNamespace.textColor = .white
    }
    
    func stopMouseOver() {
        self.bgView.flBackgroundColor = .white
        self.lblNamespace.textColor = .lightGray
    }
}
